package com.goods.controller;

import com.goods.entity.Goods;
import com.goods.model.Page;
import com.goods.model.ResponseData;
import com.goods.service.GoodsService;
import com.goods.util.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 商品接口
 * @author huangyanzhong
 * @date 2020-04-01
 */
@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController{

    private Logger logger=LoggerFactory.getLogger(getClass());

    @Autowired
    GoodsService goodsService;

    /**
     * 商品页面
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public String indexPage() {
        return "goods";
    }

    /**
     * 新建商品
     * @param goods
     * @return
     */
    @PostMapping("/create")
    @ResponseBody
    public Object create(@ModelAttribute Goods goods) {
        try {
            goodsService.insert(goods);
        }catch (Exception e){
            return failureResponse(e.getMessage());
        }
        return successResponse();
    }

    /**
     * 修改商品
     * @param goods
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public Object update(@ModelAttribute Goods goods) {
        if(goods.getId()==null){
            return failureResponse("主键不能为空");
        }
        try {
            goodsService.update(goods);
        }catch (Exception e){
            return failureResponse(e.getMessage());
        }
        return successResponse();
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public Object delete(@RequestParam("id")String id) {
        try {
            goodsService.delete(id);
        }catch (Exception e){
            return failureResponse(e.getMessage());
        }
        return successResponse();
    }

    /**
     * 获取商品详情
     * @param id
     * @return
     */
    @GetMapping("/get")
    @ResponseBody
    public Object get(@RequestParam("id")String id) {
        try {
            Goods goods=goodsService.get(id);
            return successResponse(goods);
        }catch (Exception e){
            return failureResponse(e.getMessage());
        }
    }

    /**
     * 分页查询商品
     * @param
     * @return
     */
    @GetMapping("/queryByPage")
    @ResponseBody
    public Object queryByPage(@ModelAttribute Page page,@ModelAttribute Goods goods) {
        Map<String,Object> resultMap=new HashMap<>();
        try {
            List<Goods> rows=goodsService.query(goods,page);
            long total=goodsService.count(goods);
            resultMap.put("rows",rows);
            resultMap.put("total",total);
        }catch (Exception e){
            logger.error(e.getMessage());
            resultMap.put("rows",new ArrayList<>());
            resultMap.put("total",0);
        }
        return resultMap;
    }

    /**
     * 导出
     * @param response
     * @param goods
     * @return
     */
    @RequestMapping(value="/export")
    public void export(HttpServletResponse response, @ModelAttribute Goods goods){
        response.setContentType("application/binary;charset=UTF-8");
        try{
            ServletOutputStream out=response.getOutputStream();
            try {
                response.setHeader("Content-Disposition",
                        "attachment;fileName=" + URLEncoder.encode("商品"+".xls", "UTF-8"));
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }
            String[] titles = { "ID", "商品编号", "商品名称", "商品类别","商品库存","商品单价","商品状态","创建人","创建时间" };
            String[] names = { "id", "goodsNo", "name", "categoryName","quantity","price","status","createBy","createTime" };
            List<Goods> rows=goodsService.query(goods,null);
            ExcelUtil.export(titles,names,rows, out);
        } catch(Exception e){
            throw new RuntimeException(e);
        }
    }

}